package com.zyh.shortlick.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zyh.shortlick.admin.common.convention.result.Result;
import com.zyh.shortlick.admin.common.convention.result.Results;
import com.zyh.shortlick.admin.dto.req.UserLoginReqDTO;
import com.zyh.shortlick.admin.dto.req.UserRegisterReqDTO;
import com.zyh.shortlick.admin.dto.req.UserUpdateReqDTO;
import com.zyh.shortlick.admin.dto.resp.UserActualRespDto;
import com.zyh.shortlick.admin.dto.resp.UserLoginRespDTO;
import com.zyh.shortlick.admin.dto.resp.UserRespDto;
import com.zyh.shortlick.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
//这个注解是为了userService的注入
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    //    根据用户名获取用户脱敏后的信息
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDto> getUserByUserName(@PathVariable String username){
        UserRespDto userDto = userService.getUserByUserName(username);
        return Results.success(userDto);
    }

    //    根据用户名获取用户未脱敏的信息
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDto> getActualUserByUserName(@PathVariable("username") String username){
        UserRespDto userDto = userService.getUserByUserName(username);
//        返回userRespDto会经过json的序列化器，要使用BeanUtil进行拷贝
        UserActualRespDto userActualRespDto = BeanUtil.copyProperties(userDto, UserActualRespDto.class);
        return Results.success(userActualRespDto);
    }

    /**
     * 查询用户名是否可用，用户名可用返回true，不可用返回false
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        Boolean flag=userService.hasUsername(username);
        return Results.success(flag);
    }

    /**
     * 注册用户
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 更改用户个人信息
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success();
    }

}


